<template>
	<page-meta><navigation-bar title="绑定实盘" front-color="#000000" background-color="#ffffff" /></page-meta>
	<view :style="{'min-height':height+'px'}" style="background: #f7f7f7;padding-top: 15px;">
			<view>
				<view @tap="open(i);" class="" v-for="i,index in shipandata" :style="{'margin-top':index!=0?'15px':0}" style="background: #fff;width: calc(100% - 70px);margin: 0 auto;padding: 15px;border-radius: 8px;position: relative;">
					<text style="font-size: 13px;color: #acacac;">{{(i.addtime).replace('T',' ')}}</text>
					<view style="font-size: 16px;color: #000;padding-top: 8px;padding-bottom: 2px;">
						{{i.traderApi?i.traderApi:'--'}}
						<text style="float: right;color: #000000;font-size: 17px;">{{i.accountNo}}</text>
					</view>
					<text style="font-size: 14px;color: #acacac;">{{i.dealer?i.dealer:'--'}}</text>
					<text style="position: absolute;right: -6px;top: 10px;background:#168deb ;padding: 0 10px;display: block;color: #fff;font-size: 15px;border-radius: 3px;">
						{{i.state == 1?'在线':'离线'}}
					</text>
				</view>
			</view>
			<uni-text v-if="!shipandata.length" style="height: 400x;font-size: 16px;text-align: center;display: block;color: red;padding-top: 150px;">
				暂无实盘信息！
				<!-- <navigator url="../center/setshi" style="color:#168deb ;display: inline-block;">去添加</navigator> -->
			</uni-text>
			<!-- <view>
				<button style="position: fixed;z-index: 10;top: 10px;line-height: 25px;right:20px ;font-size: 14px;border-radius: 0; background: #168deb;color: #fff;" type="default">添加实盘</button>
			</view> -->
			<!-- <view class="" style="background: #fff;width: calc(100% - 70px);margin: 0 auto;padding: 15px;border-radius: 8px;position: relative;margin-top: 15px;">
				<text style="font-size: 13px;color: #cccccc;">2019/10/08</text>
				<view style="font-size: 16px;color: #000;padding-top: 8px;padding-bottom: 2px;">
					易胜9.0
					<text style="float: right;color: #000000;font-size: 17px;">Q12321321332123</text>
				</view>
				<text style="font-size: 14px;color: #ccc;">新湖期货</text>
				<text style="position: absolute;right: -10px;top: 10px;background:#168deb ;padding: 0 10px;display: block;color: #fff;font-size: 15px;border-radius: 3px;">在线</text>
			</view> -->
			<uni-popup type="bottom" ref="settype">
				<view style="background: #fff;padding: 20px 25px;">
					<button type="default" v-if="nowobj.state == 1" style="background: #168deb;color: #fff;margin-bottom: 10px;" @tap="shilogin">退出</button>
					<button type="default" v-else style="background: #168deb;color: #fff;margin-bottom: 10px;" @tap="shilogin">登录</button>
					<button type="default" style="background: none;color: #168deb;margin-bottom: 10px;border-color: #168deb;" @tap="set">修改</button>
					<button type="default" style="color: red;" @tap="del">删除</button>
				</view>
			</uni-popup>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				Url:this.$store.state.url1,
				height:0,
				shipandata:[],
				jklst:[],
				sojyslst:[],
				Times:{},
				nowobj:{}
			}
		},
		onNavigationBarButtonTap(e){
			this.$go('/pages/center/addshi');
		},
		methods: {
			
			open(obj) {
				this.$refs.settype.open();
				this.nowobj = obj;
			},
			close() {
				this.$refs.settype.close();
			},
			getdata(){
				var _this = this;
				_this.GetLoginState();
				_this.getshipan();
				_this.ByApiIdso();
				_this.getname();
			},
			del(){
				var _this = this;
				uni.showModal({
				    title: 'App提示',
				    content: '是否删除实盘账号'+this.nowobj.accountNo,
				    success: function (res) {
				        if (res.confirm) {
				            _this.shidel()
				        } else if (res.cancel) {
				            _this.close();
				        }
				    }
				});
			},
			set(){
				this.$go('./addshi?obj='+JSON.stringify(this.nowobj));
			},
			shidel:function(){//实盘删除
				var _this = this;
				var obj = _this.nowobj;
				uni.request({
					method: 'get',
					url: _this.Url + '/api/DealerAccount/del?token=' + _this.$store.state.user.token+'&id='+obj.id,
					// contentType: "application/json;",
					// data: JSON.stringify(_this.shisetdata),
					success: function(data) {
						// console.log(data)
						data = data.data;
						if (data.code == 200) {
							for(var i in _this.shipandata){
								var o = _this.shipandata[i];
								if(o.accountNo == obj.accountNo){
									_this.shipandata.splice(i,1);	
								}
							}
							_this.close();
						} else {
							
						}
					}
				})
			},
			shilogin:function(){//实盘登录退出
			    var obj = this.nowobj;
				var stat = 1;
				if(obj.state == 1){
					stat = 2
				};
				var _this = this;
				uni.request({
					method: 'get',
					url: _this.Url + '/api/DealerAccount/login?token=' + _this.$store.state.user.token+'&id='+obj.id+'&state='+stat,
					// contentType: "application/json;",
					// data: JSON.stringify(_this.shisetdata),
					success: function(data) {
						
						data = data.data;
						if (data.code == 200) {
							obj.state = stat;
							_this.close();
						} else {
							uni.showToast({
								title: data.msg,
								icon: 'none',
								duration: 2000
								// position:'top'
							});
						}
					}
				})
			},
			getshipan: function() { //实盘列表
				var _this = this;
				var json = {
					token: _this.$store.state.user.token
					// accounts:''
				};
				uni.request({
					method: 'get',
					url: _this.Url + '/api/DealerAccount/list?token=' + _this.$store.state.user.token,
					// contentType: "application/json;",
					// data: json,
					success: function(data) {
						data = data.data;
						// console.log(data.data)
						// console.log(data.data)
						if (data.code == 200) {
							_this.shipandata = data.data;
							// console.log(_this.shipandata)
							for (var i in _this.shipandata) {
								let o = _this.shipandata[i];
								_this.resetnane(o);
							}
						} else {
							// alert(data.msg)
						}
					}
				})
			},
			GetLoginState: function() { //登录状态
				var _this = this;
				this.Times = setInterval(function() {
					for(var i in _this.shipandata){
						let obj = _this.shipandata[i];
						uni.request({
							method: 'get',
							url: _this.Url + '/api/DealerAccount/GetLoginState?token=' + _this.$store.state.user.token + '&ids=' + obj.id,
							// contentType: "application/json;",
							// data: json,
							success: function(data) {
								// console.log(data.data , '登录信息')
								data= data.data;
								if (data.code == 200) {
									for (var i in data.data) {
										var o = data.data[i];
										// console.log(o)
										if (obj.id == o.id) {
											obj.loginRemark = o.loginRemark;
											obj.state = o.state;
											// console.log(o.state)
											break;
										}
									}
								} else {
									// alert(data.msg)
								}
							}
						})
					}
					
				}, 5000);
			},
			getname: function(obj) { //接口类型
				var _this = this;
				uni.request({
					method: 'get',
					url: _this.Url + '/api/TraderApis/getNames?token=' + _this.$store.state.user.token,
					// contentType: "application/json;",
					// data: json,
					success: function(data) {
						// console.log(data.data,'接口类型list')
						data= data.data;
						if (data.code == 200) {
							_this.jklst = data.data;
							
						} else {
							// alert(data.msg)
						}
					}
				})
			},
			ByApiIdso: function() { //期货商
				var _this = this;
				uni.request({
					method: 'get',
					url: _this.Url + '/api/Dealers/GetDealersByApiId?token=' + _this.$store.state.user.token + '&apiId=0',
					// contentType: "application/json;",
					// data: json,
					success: function(data) {
						// console.log(data.data , '期货商list')
						data= data.data;
						if (data.code == 200) {
							_this.sojyslst = data.data;
						} else {
							// alert(data.msg)
						}
					}
				})
			},
			resetnane:function(obj){
				var _this = this;
				setTimeout(function(){
					for (var i in _this.jklst) {
						var o = _this.jklst[i];
						// console.log(obj.traderApiID , o.id)
						if (obj.traderApiID == o.id) {
							obj.traderApi = o.name;
							break;
						}
					}
					// console.log(_this.jyslst,obj.dealerID)
					for (var c in _this.sojyslst) {
						var o = _this.sojyslst[c];
						// console.log(obj.dealerID , o.id)
						if (obj.dealerID == o.id) {
							obj.dealer = o.name;
							break;
						}
					}
				},500)
			}
		},
		onShow() {
			var _this = this;
			uni.getNetworkType({
				success: function(res) {
					if (res.networkType == 'none') {
						uni.showToast({
							title: '当前无网络连接，请检查！',
							icon: 'none',
							duration: 5000
			
							// position:'top'
						});
					}
				}
			});
			uni.onNetworkStatusChange(function(res) {
				if (!res.isConnected) {
					uni.showToast({
						title: '当前无网络连接，请检查！',
						icon: 'none',
						duration: 3000
						// position:'top'
					});
				} else {
					uni.showToast({
						title: '网络已连接！',
						icon: 'none',
						duration: 3000
						// position:'top'
					});
					_this.getdata();
				}
			});
			this.getdata();
			this.height = uni.getSystemInfoSync().windowHeight;
		},
		onHide() {
			clearInterval(this.Times);
			this.$refs.settype.close();
		}
	}
</script>

<style>

</style>
